import request from '@/utils/request'

function getCourseTable(userId) {
  return request({
    url: userId ? '/course/userId': '/course/mine',
    method: 'get',
  })
}

function transformCourseOptionData(rawData) {
  for(let college of rawData){
    college.value = college.id.toString();
    college.label = college.name
    for(let major of college.majors) {
      major.value = major.id.toString();
      major.label = major.name;
      for(let subject of major.subjects) {
        subject.value = subject.id.toString()
        subject.label = subject.name
      }
      major.children = major.subjects;
    }
    college.children = college.majors;
  }
  return rawData;
}

function getCourseOptions() {
  return request({
    url: '/course/',
    method: 'get'
  }).then((rawData)=>{
    return transformCourseOptionData(rawData.data);
  })
}

export default {
  getCourseTable,
  getCourseOptions
}
